import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, UpdateDateColumn, CreateDateColumn, OneToMany, ManyToMany, JoinTable, Index } from 'typeorm';
import { UserAccount } from 'src/user_account/entities/user_account.entity'
import { ArticleTag } from './article_tags.entity';
import { ArticleComment } from './comment.entity';
@Entity({ name: 'article_info' })
export class Article {
    @PrimaryGeneratedColumn()
    id: number

    @Column({ length: 255, type: String, nullable: false, comment: "文章标题" })
    @Index('fulltext_idx', { fulltext: true })
    title: string;

    @Column({ type: 'text', nullable: false, comment: "文章内容" })
    content: string;

    @Column({ length: 255, type: String, nullable: false, comment: "文章分类" })
    category: string;

    @Column({ length: 255, type: String, nullable: false, comment: "文章作者" })
    author: string;

    @Column({ length: 255, type: String, nullable: false, comment: "文章简介" })
    introduction: string;

    @Column({ length: 255, type: String, nullable: false, comment: "文章状态", default: "待审核" })
    publish_status: string;

    @Column({ type: Number, nullable: false, comment: "文章浏览量", default: 0 })
    views: number;

    @Column({ type: Number, nullable: false, comment: "文章点赞量", default: 0 })
    likes: number;

    @Column({ length: 255, type: String, comment: "文章收藏量", default: 0 })
    collections: number;

    @Column({ type: 'enum', enum: ['私有', '公开'], comment: '权限', default: '公开' })
    right: '私有' | '公开'


    @CreateDateColumn({ type: "timestamp", name: 'create_time' })
    createTime: Date;

    @UpdateDateColumn({
        name: 'update_time',
        nullable: true,
    })
    updateTime: Date | null;


    @ManyToOne(type => UserAccount, userAccount => userAccount.articles, { onDelete: 'CASCADE' })
    @JoinColumn()
    userAccount: UserAccount

    @OneToMany(type => ArticleTag, tags => tags.article, { onDelete: 'CASCADE' })
    tags: ArticleTag[];


    @ManyToMany(type => UserAccount, beCollected => beCollected.collections, { onDelete: 'CASCADE' })
    beCollected: UserAccount[]

    @ManyToMany(type => UserAccount, liker => liker.likes)
    liker: UserAccount[]


    @ManyToMany(type => UserAccount, vierers => vierers.views, { onDelete: 'CASCADE' })
    viewer: UserAccount[]


    @OneToMany(type => ArticleComment, comment => comment.article, { cascade: true, onDelete: 'CASCADE' })
    comment: ArticleComment[]
}
